package com.jsgt.greekfire.ui.wallet.add

import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.YTWallet
import com.jsgt.greekfire.common.util.YTWalletUtils
import com.jsgt.greekfire.dao.model.IdentityBean
import kotlinx.android.synthetic.main.activity_import_wallet_mnemonic.*
import org.jetbrains.anko.hintResource

class ImportWalletKeyActivity:ImportWalletMnemonicActivity() {

    override fun initInputContent() {
        txvType.text = getString(R.string.private_key)
        edtContent.hintResource = R.string.input_private_key
    }

    override fun checkContent(): Boolean {
        if (edtContent.text.isNullOrEmpty()) {
            ToastUtils.show(R.string.input_private_key_address)
            return false
        }
        return true
    }

    override fun getWallet(): YTWallet? {
        return YTWalletUtils.generateBip44WalletByPrivateKey(edtContent.text.toString(), null)
    }

    override fun save(wallet: YTWallet?) {
        try {
            if (wallet == null) {
                showProgress(false)
                ToastUtils.show(this, getString(R.string.input_private_key_address_error))
                txvOk.isEnabled = true
            } else {
                val identityBean = IdentityBean()
//                identityBean.setMnemonicByAES(wallet.getMnemonic())
                identityBean.setPriKeyByAES(wallet.getEcKeyPair().privateKey.toString())
                identityBean.setPasswordByMd5(edtPassword.text.toString())
//                walletBean.identityName = walletName
                identityBean.backup = true
                if (edtPasswordTips.text.isNotEmpty()) {
                    identityBean.passwordTips = edtPasswordTips.text.toString()
                }
                walletViewModel.addIdentity(identityBean)
            }
        } catch (e: Exception) {
            showProgress(false)
            ToastUtils.show(this, getString(R.string.input_private_key_address_error))
            txvOk.isEnabled = true
        }
    }
}